package image.mapper;

import image.PixelImage;
import image.color.Color_Float;
import math.mathExtended.MathExtended;
import math.matrix.Matrix;
import math.matrix.MatrixMapper;

/**
 *
 * Fully Tested: No
 * Fully Documented: No
 * Created: 24-May-2012
 * Last Updated: 24-May-2012
 *
 * @author Shimu Wu
 */
public class HorizontalKernelMapper implements PixelImageMapper {

    private final float[] kernel;

    private final int midX;

    public HorizontalKernelMapper(float[] kernel) {
        this.kernel = kernel;
        this.midX = kernel.length / 2;
    }

    @Override
    public int process(int originalX, int originalY,
            PixelImage original) {

        if (!MathExtended.bounded(originalX, midX, original.getWidth()
                - midX - 1)) {
            return original.get(originalX, originalY);
        }

        // accumulator
        Color_Float ac = new Color_Float(0, 0, 0);
        Color_Float temp; 
        int kernelIndex = 0;

        for (int x = -midX; x <= midX; x++) {
                temp = new Color_Float(original.get(originalX + x,
                        originalY));
                temp.scale(kernel[kernelIndex]);
                ac.add(temp);
                kernelIndex++;
        }
        ac.clamp();
        return ac.intRep();
    }
}
